import LayerData from "../layerdata";
import FrameData from "./FrameData";
import AnimationData from "./AnimationData";

export default class AnimationManager {
    constructor() {
        this.data = {};
        AnimationManager.instance = this;
        this.data = {};
    }

}
AnimationManager.prototype.putAnimation = function (name, data, n) {
    var r = this.parseAnimation(data, n);
    this.data[name] = r;
    return r;
};
AnimationManager.prototype.parseAnimation = function (e, num) {
    var anm = new AnimationData(e.name, null);
    //        var maxFrame = 0;
    var len = e.l.length == undefined ? 1 : e.l.length;
    for (var idx = 0; idx < len; ++idx) {
        var spdata = len == 1 ? e.l : e.l[idx];
        var fname = spdata.flashName;
        var isMark = spdata.isMark != undefined;
        var layerData = new LayerData(spdata.name, fname ? fname : "", isMark);
        var l = spdata.d.length == undefined ? 1 : spdata.d.length;
        for (var c = 0; c < l; ++c) {
            var anchor = l == 1 ? spdata.d : spdata.d[c];
            var p = anchor.name;
            var d = p.split("/");
            var pname = d[d.length - 1]; //如果有目录.获取目录名称
            var anchorX = parseFloat(anchor.anchorX);
            var anchorY = parseFloat(anchor.anchorY);
            layerData.addClipData(pname,new Laya.Point(anchorX, anchorY > 0 ? 1 - anchorY : 1 - anchorY));
        }
        for (var i = 0; i < num; i++) {
            layerData.addFrame(FrameData.getEmptyData());
        }
        var flen = spdata.f.length == undefined ? 1 : spdata.f.length;
        for (var fidx = 0; fidx < flen; ++fidx) {
            var frameData = null;
            var fData = flen == 1 ? spdata.f : spdata.f[fidx];
            var visible = fData.v != undefined && fData.v != null ? fData.v : true;
            if (visible) {
                var alpha = fData.a != undefined ? parseFloat(fData.a) : 100;
                alpha /= 100;
                var rotation = parseFloat(fData.r);
                var x = parseFloat(fData.x);
                var y = parseFloat(fData.y);
                var scaleX = parseFloat(fData.scX);
                var scaleY = parseFloat(fData.scY);
                var skewX = parseFloat(fData.skX);
                var skewY = parseFloat(fData.skY);
                frameData = new FrameData(visible, alpha, rotation, x, y, scaleX, scaleY, skewX, skewY);
            } else {
                frameData = FrameData.getEmptyData();
            }
            layerData.addFrame(frameData);
        }
        anm.addLayer(layerData);
        //            maxFrame = Math.max(maxFrame, fidx);
    }
    anm.makeTheSameAmountOfFramesInAllLayers();
    return anm;
};
AnimationManager.prototype.getAnimation = function (e) {
    return this.data[e];
};